package com.zhijian.medical.service.impl;

import com.zhijian.medical.dao.local.TopicHistoryMapper;
import com.zhijian.medical.entity.po.TopicHistory;
import com.zhijian.medical.entity.vo.request.TopicHistorySearchVo;
import com.zhijian.medical.entity.vo.response.TopicHistoryVo;
import com.zhijian.medical.service.TopicHistoryService;
import com.zhijian.medical.util.Page;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * 新文件管理修改历史记录业务处理层
 *
 * @author HCG
 * @version: 1.0
 * @date 2022-07-31 13:57:36
 */
@Slf4j
@Service
public class TopicHistoryServiceImpl implements TopicHistoryService {

    @Autowired
    private TopicHistoryMapper mapper;

    @Override
    public TopicHistory selectById(Long id) {
        return mapper.selectByPrimaryKey(id);
    }

    @Override
    public TopicHistoryVo selectVoById(Long id) {
        return mapper.selectVoById(id);
    }

    @Override
    public void deleteById(Long id) {
        mapper.deleteByPrimaryKey(id);
    }

    @Override
    public Long insert(TopicHistory topicHistory) {
        Date now = new Date();
        topicHistory.setCreateTime(now);
        topicHistory.setUpdateTime(now);
        return mapper.insert(topicHistory) > 0 ? topicHistory.getId() : 0L;
    }

    @Override
    public boolean update(TopicHistory topicHistory) {
        Date now = new Date();
        topicHistory.setUpdateTime(now);
        return mapper.updateByPrimaryKey(topicHistory) != 0;
    }

    @Override
    public Integer count(TopicHistorySearchVo topicHistorySearchVo) {
        return mapper.count(topicHistorySearchVo);
    }

    @Override
    public List<TopicHistoryVo> selectList(TopicHistorySearchVo topicHistorySearchVo) {
        return mapper.selectList(topicHistorySearchVo);
    }

    @Override
    public Page<TopicHistoryVo> selectPage(TopicHistorySearchVo topicHistorySearchVo) {
        //先直接查总数
        int count = count(topicHistorySearchVo);
        //分页
        Page<TopicHistoryVo> page = new Page<>(true, count, topicHistorySearchVo.getPageNum(), topicHistorySearchVo.getPageSize());
        topicHistorySearchVo.setStartIndex(page.getStartIndex());
        page.setPage(true);
        page.setTotalCount(count);
        List<TopicHistoryVo> responseVos = this.selectList(topicHistorySearchVo);
        page.setData(responseVos);
        return page;
    }

}